home *** CD-ROM | disk | FTP | other *** search
/ Network Support Library / RoseWare - Network Support Library.iso / print / 890ps.arc / LC890.C next >
Text File  |  1990-01-08  |  10KB  |  378 lines

  1. /*  Program to service print jobs.  */
  2.  
  3.  
  4. #define MAXQUEUE     25
  5. #define WORK_SPACE   0x1000
  6. #define DELAY_SCALE  18
  7. #define TRUE         1
  8. #define FALSE        0
  9. #define PRINT_SERVER 0x7
  10. #define PRINT_QUEUE  0x3
  11. #define LPT1         0x1
  12. #define NUL          0x0
  13. #define ESC          0x1B
  14. #define SP           0x20
  15. #define _JPI_WIN_
  16.  
  17.  
  18. #include <conio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <netware.h>
  22. #include <dos.h>
  23. #include <io.h>
  24. #include <fcntl.h>
  25. #include <process.h>
  26.  
  27.  
  28. SIGNAL UpdateSignal,
  29.        PrintSignal;
  30. unsigned TickleTime = 7*DELAY_SCALE,  /* Must be locked when accessed by other than Tickler */
  31.          CurrQueue;                   /* Must be locked when accessed by other than Printer */
  32. wintype StatusWin, BlankWin;
  33. struct {                              /* Must be locked when accessed by other than Updater or Printer */
  34.   char queue[25];
  35.   dword id;
  36.   char PreMode[50];
  37.   unsigned PreWait;
  38.   unsigned MidWait;
  39.   char PostMode[50];
  40.   unsigned PostWait;
  41.   char Desc[25];
  42.   int port;
  43.   wintype win;
  44.   int print;
  45.   int display;
  46. } QueueInfo[MAXQUEUE];
  47. windef DefWinDef = {0,0,SCREENWIDTH-1,SCREENDEPTH-1,LightGray,Black,FALSE,FALSE,FALSE,FALSE};
  48.  
  49.  
  50. void out(const char *data, unsigned len, int port)
  51. {
  52.   union REGS i_regs,o_regs;
  53.  
  54.   if (port>0) {
  55.     i_regs.h.ah = 0x00;
  56.     i_regs.x.dx = port-1;
  57.   } else {
  58.     if (port<0) {
  59.       /**/ /* handle async port */
  60.     }
  61.   }
  62.   while (len!=0) {
  63.     if (*data!=0) {
  64.       if (port>0) {
  65.         i_regs.h.al = *data;
  66.         do {
  67.           int86(0x17,&i_regs,&o_regs);
  68.         } while ((o_regs.h.ah&0x01)==1);
  69.       } else {
  70.         if (port<0) {
  71.           /**/ /* handle async port */
  72.         }
  73.       }
  74.     }
  75.     data++;
  76.     len--;
  77.   }
  78. }
  79.  
  80.  
  81. void Tickler(void)
  82.   /* Must not alter TickleTime */
  83. {
  84.   while (1) {
  85.     Delay(TickleTime);
  86.     Notify(UpdateSignal);
  87.   }
  88. }
  89.  
  90.  
  91. void Updater(void)
  92.   /* Must not alter QueueInfo */
  93. {
  94.   unsigned queue;
  95.   word count,jobs[250],idx;
  96.   PrintJobClientStruct client;
  97.   dword size;
  98.   char desc[50];
  99.   QueueJobInfoStruct info;
  100.   int blank, found;
  101.  
  102.   blank = 0;
  103.   while (1) {
  104.     if (blank >= 2) {
  105.       blank = 0;
  106.       putontop(BlankWin);
  107.     }
  108.     WAIT(UpdateSignal);
  109.     found = FALSE;
  110.     for (queue=0;queue<MAXQUEUE;queue++) {
  111.       if (QueueInfo[queue].display) {
  112.         GetQueueJobList(QueueInfo[queue].id,&count,jobs,NULL);
  113.         if (count!=0) {
  114.           hide(BlankWin);
  115.           Notify(PrintSignal);
  116.           blank = 0;
  117.           found = TRUE;
  118.         }
  119.         use(QueueInfo[queue].win);
  120.         clrscr();
  121.         for (idx=count;idx>0;idx--) {
  122.           if ((ReadQueueJobEntry(QueueInfo[queue].id,jobs[idx-1],&info,desc,NULL)==0) &&
  123.               (ReadPrintJobInfo(QueueInfo[queue].id,jobs[idx-1],&client)==0) &&
  124.               (GetQueueJobsFileSize(QueueInfo[queue].id,jobs[idx-1],&size)==0)) {
  125.             gotoxy(1,1);
  126.             insline();
  127.             if (info._JobFlags&0x20) {
  128.               cputs("~");
  129.             } else {
  130.               if (info._ServerID!=0) {
  131.                 cputs("\xAF");
  132.               } else {
  133.                 cputs(" ");
  134.               }
  135.             }
  136.             cprintf("%s->%s %lu",client._BannerName,desc,size);
  137.           }
  138.         }
  139.       }
  140.     }
  141.     if (!found) {
  142.       ++blank;
  143.     }
  144.   }
  145. }
  146.  
  147.  
  148. void Printer(void)
  149.   /* Must not alter QueueInfo */
  150. {
  151.   unsigned queue;
  152.   QueueJobInfoStruct info;
  153.   int file;
  154.   int len;
  155.   char buf[1024];
  156.   PrintJobClientStruct client;
  157.   dword size;
  158.   char desc[50];
  159.  
  160.   use(StatusWin);
  161.   while (1) {
  162.     WAIT(PrintSignal);
  163.     queue = CurrQueue;
  164.     do {
  165.       if (QueueInfo[queue].print) {
  166.         while (ServiceQueueJobAndOpenFile(QueueInfo[queue].id,0xFFFF,&info)==0) {
  167.           Notify(UpdateSignal);
  168.           if (queue!=CurrQueue) {
  169.             cprintf("  Switching from %s to %s...",QueueInfo[CurrQueue].Desc,QueueInfo[queue].Desc);
  170.             Delay(QueueInfo[CurrQueue].MidWait*DELAY_SCALE);
  171.             out(QueueInfo[CurrQueue].PostMode,strlen(QueueInfo[CurrQueue].PostMode),QueueInfo[queue].port);
  172.             Delay(QueueInfo[CurrQueue].PostWait*DELAY_SCALE);
  173.             setframe(QueueInfo[CurrQueue].win,DOUBLEFRAME,LightGray,Black);
  174.             out(QueueInfo[queue].PreMode,strlen(QueueInfo[queue].PreMode),QueueInfo[queue].port);
  175.             Delay(QueueInfo[queue].PreWait*DELAY_SCALE);
  176.             CurrQueue = queue;
  177.             cputs("Done.\n");
  178.             setframe(QueueInfo[CurrQueue].win,DOUBLEFRAME,Black,LightGray);
  179.           }
  180.           ReadQueueJobEntry(QueueInfo[queue].id,info._JobNumber,NULL,desc,NULL);
  181.           ReadPrintJobInfo(QueueInfo[queue].id,info._JobNumber,&client);
  182.           GetQueueJobsFileSize(QueueInfo[queue].id,info._JobNumber,&size);
  183.           cprintf("  Printing %s->%s (%lu)...          ",client._BannerName,client._BannerFile,size);
  184.           size = 0;
  185.           Lock();
  186.           if((file=_open("NETQ",O_RDONLY|SH_NOINHERIT|SH_DENYRW))!=-1) {
  187.             while ((len=_read(file,buf,sizeof(buf)))>0) {
  188.               Unlock();
  189.               out(buf,len,QueueInfo[queue].port);
  190.               size += len;
  191.               cprintf("\b\b\b\b\b\b\b\b\b\b%-10lu",size);
  192.               Lock();
  193.             }
  194.             _close(file);
  195.           }
  196.           Unlock();
  197.           if ((client._Flags&0x8)==0) {
  198.             out("\xC",1,QueueInfo[queue].port);
  199.           }
  200.           FinishServicingQueueJobAndFile(QueueInfo[queue].id,info._JobNumber,0);
  201.           cputs("\b\b\b\b\b\b\b\b\b\bDone.     \n");
  202.         }
  203.         Notify(UpdateSignal);
  204.       }
  205.       queue++;
  206.       if (queue==MAXQUEUE) {
  207.         queue = 0;
  208.       }
  209.     } while (queue!=CurrQueue);
  210.   }
  211. }
  212.  
  213.  
  214. unsigned AddQueue(const char *queue, const char *Desc, int print)
  215. {
  216.   unsigned idx;
  217.   wintype twin;
  218.   static abscoord x = 0,
  219.                   y = 0;
  220.   dword id;
  221.  
  222.   if (GetBinderyObjectID(queue,PRINT_QUEUE,&id)==0) {
  223.     for (idx=0;idx<MAXQUEUE;idx++) {
  224.       if (!QueueInfo[idx].display) {
  225.         Lock();
  226.         strcpy(QueueInfo[idx].queue,queue);
  227.         QueueInfo[idx].id = id;
  228.         strcpy(QueueInfo[idx].Desc,Desc);
  229.         twin = used();
  230.         QueueInfo[idx].win = windowopen(&DefWinDef);
  231.         change(QueueInfo[idx].win,x,y,x+33,y+5);
  232.         if (print) {
  233.           setframe(QueueInfo[idx].win,DOUBLEFRAME,LightGray,Black);
  234.         } else {
  235.           setframe(QueueInfo[idx].win,SINGLEFRAME,LightGray,Black);
  236.         }
  237.         settitle(QueueInfo[idx].win,QueueInfo[idx].Desc,LeftUpperTitle);
  238.         textcolor(White);
  239.         textbackground(Black);
  240.         clrscr();
  241.         use(twin);
  242.         x += 35;
  243.         if ((x+35)>=SCREENWIDTH) {
  244.           x = (x % 35)+2;
  245.           y += 6;
  246.         }
  247.         QueueInfo[idx].print = print;
  248.         QueueInfo[idx].display = TRUE;
  249.         break;
  250.       }
  251.     }
  252.   } else {
  253.     idx = MAXQUEUE;
  254.   }
  255.   return(idx);
  256. }
  257.  
  258.  
  259. void AddDisplayQueue(const char *queue, const char *Desc)
  260. {
  261.   unsigned idx;
  262.  
  263.   idx = AddQueue(queue,Desc,FALSE);
  264.   if (idx<MAXQUEUE) {
  265.     Unlock();
  266.   }
  267. }
  268.  
  269.  
  270. void AddPrintQueue(const char *queue, const char *PreMode, unsigned PreWait,
  271.                    unsigned MidWait, const char *PostMode, unsigned PostWait,
  272.                    const char *Desc, int port)
  273. {
  274.   unsigned idx;
  275.  
  276.   idx = AddQueue(queue,Desc,TRUE);
  277.   if (idx<MAXQUEUE) {
  278.     strcpy(QueueInfo[idx].PreMode,PreMode);
  279.     QueueInfo[idx].PreWait = PreWait;
  280.     QueueInfo[idx].MidWait = MidWait;
  281.     strcpy(QueueInfo[idx].PostMode,PostMode);
  282.     QueueInfo[idx].PostWait = PostWait;
  283.     QueueInfo[idx].port = port;
  284.     AttachQueueServerToQueue(QueueInfo[idx].id);
  285.     Unlock();
  286.   }
  287. }
  288.  
  289.  
  290. void CleanUp(void)
  291. {
  292.   unsigned idx;
  293.  
  294.   for (idx=0;idx<MAXQUEUE;idx++) {
  295.     if (QueueInfo[idx].display) {
  296.       if (QueueInfo[idx].print) {
  297.         DetachQueueServerFromQueue(QueueInfo[idx].id);
  298.       }
  299.       windowclose(QueueInfo[idx].win);
  300.     }
  301.   }
  302. }
  303.  
  304.  
  305. main()
  306. {
  307.   int cont;
  308.  
  309.   clrscr();
  310.   BlankWin = windowopen(&DefWinDef);
  311.   change(BlankWin,0,0,SCREENWIDTH-1,SCREENDEPTH-1);
  312.   StatusWin = windowopen(&DefWinDef);
  313.   change(StatusWin,0,0,SCREENWIDTH-1,SCREENDEPTH-1);
  314.   use(StatusWin);
  315.   gotoxy(1,25);
  316.   cputs("Login...\n");
  317.   LoginToFileServer("LC890",PRINT_SERVER,"LC890");
  318.   cputs("Attaching to queues...\n");
  319.   AddPrintQueue("POSTSCRIPT","",0,0,"",0,"PostScript",LPT1);
  320.   AddPrintQueue("POSTSCRIPTINTERACTIVE",
  321.                 "statusdict begin 1 setsoftwareiomode end\x04",4,10,
  322.                 "statusdict begin 1 setsoftwareiomode end\x04",4,
  323.                 "PostScript Interactive",LPT1);
  324.   AddPrintQueue("DIABLO630","statusdict begin 2 setsoftwareiomode end\x04",4,
  325.                 10,"\x1B\x7F\x30\x04",10,"Diablo 630",LPT1);
  326.   AddPrintQueue("LASERJET","statusdict begin 5 setsoftwareiomode end\x04",4,
  327.                 10,"\x1B\x7F\x30\x04",10,"LaserJet",LPT1);
  328.   AddDisplayQueue("LABEL","Mailing Labels");
  329.   AddDisplayQueue("LABEL_2","Big Mailing Labels");
  330.   CurrQueue = 0;  /* The queue mode which the printer is currently in */
  331.   setframe(QueueInfo[CurrQueue].win,DOUBLEFRAME,Black,LightGray);
  332.   Init(&UpdateSignal);
  333.   Init(&PrintSignal);
  334.   StartScheduler();
  335.   StartProcess(Updater,WORK_SPACE,1);
  336.   StartProcess(Printer,WORK_SPACE,1);
  337.   StartProcess(Tickler,WORK_SPACE,1);
  338.   cputs("Ready.\n");
  339.   cont = TRUE;
  340.   do {
  341.     while (!kbhit()) {
  342.       Delay(0);
  343.     }
  344.     switch (getch()) {
  345.       case NUL : while (kbhit()) {
  346.                    getch();
  347.                  }
  348.                  break;
  349.       case ESC : cont = FALSE;
  350.                  break;
  351.       case SP  : hide(BlankWin);
  352.                  break;
  353.       case '+' : Lock();
  354.                  TickleTime += DELAY_SCALE;
  355.                  Unlock();
  356.                  break;
  357.       case '-' : Lock();
  358.                  if (TickleTime>DELAY_SCALE) {
  359.                    TickleTime -= DELAY_SCALE;
  360.                  } else {
  361.                    TickleTime = 0;
  362.                  }
  363.                  Unlock();
  364.                  break;
  365.     }
  366.   } while (cont);
  367.   cputs("Stopping other processes...\n");
  368.   StopScheduler();
  369.   hide(BlankWin);
  370.   cputs("Detaching from queues...\n");
  371.   CleanUp();
  372.   cputs("Logging out...\n");
  373.   Logout();
  374.   cputs("Done.\n");
  375.   cursoron();
  376.   return(0);
  377. }
  378.